package com.springcloud.feigndemoserver1000.controller;

import com.alibaba.fastjson.JSON;
import com.springcloud.common.dto.PageDTO;
import com.springcloud.common.dto.ResponseData;
import com.springcloud.common.dto.UserDTO;
import com.springcloud.common.enums.ErrorCodeEnum;
import com.springcloud.feigndemoserver1000.servie.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Value("${server.port}")
    private Integer serverPort;

    /*
     * 测试二
     *
     * @Author ZhangBin
     * @Date 2020/6/30 13:53
     * @Description: 根据id获取用户信息
     *
     **/
    @GetMapping(value = "/getUserById/{userId}")
    public ResponseData<UserDTO> getUserById(@PathVariable(name="userId")Integer id,HttpServletRequest request){
        log.info("SERVER_PORT={},userId={}",serverPort,id);
        log.info("token={}",request.getHeader("token"));
        //int i = 1 / 0;
        UserDTO userDTO = userService.getUserById(id);
        return ResponseData.response(ErrorCodeEnum.SUCCESS,userDTO);
    }
    /*
     * @Author ZhangBin
     * @Date 2020/6/30 13:53
     * @Description: 分页查询
     *
     **/
    @RequestMapping(value = "/getUserPage",method = RequestMethod.POST)
    ResponseData<PageDTO<UserDTO>> getUserPage(PageDTO pageDTO){
        log.info("pageDTO={}", JSON.toJSONString(pageDTO));
        PageDTO<UserDTO> pages =  userService.getUserPage(pageDTO);
        return ResponseData.response(ErrorCodeEnum.SUCCESS,pages);
    }

    /*
     * @Author ZhangBin
     * @Date 2020/8/19 16:41
     * @Description: 文件上传测试
     *
     **/
    @RequestMapping(value = "/fileUploadTest",method = RequestMethod.POST)
    public ResponseData<?> fileUploadTest(@RequestPart(value = "file") MultipartFile file,String remark){
        log.info("开始文件上传,remakr={}",remark);
        if(file == null || file.getSize()<=0){
            log.info("文件大小不满足要求");
            return ResponseData.response(ErrorCodeEnum.FAIL,null);
        }
        try {
            file.transferTo(new File("d://"+file.getOriginalFilename()));
        } catch (IOException e) {
            log.error("文件上传失败",e);
            return ResponseData.response(ErrorCodeEnum.FAIL,null);
        }
        log.info("文件上传结束");
        return ResponseData.response(ErrorCodeEnum.SUCCESS,null);
    }
    /*
     * @Author ZhangBin
     * @Date 2020/8/20 17:05
     * @Description: 图片流返回测试，如：图形验证码
     *
     **/
    @RequestMapping("/imgIOStreamTest")
    public void imgIOStreamTest(HttpServletResponse httpServletResponse) throws IOException {
        ServletOutputStream out = null;
        InputStream inputStream = null;
        try {
            //获取图片存放路径
            ClassPathResource classPathResource = new ClassPathResource("/static/feign-图片流测试图片.jpg");
            //ClassPathResource classPathResource = new ClassPathResource("/static/feign-文件流测试.txt");
            inputStream = classPathResource.getInputStream();
            out = httpServletResponse.getOutputStream();
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = inputStream.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.close();
            inputStream.close();
        }
    }
}
